<?php

namespace Biz\model;

use Medoo\Medoo;
use Biz\validate\ShopErpInfo as vShopErpInfo;
use Erp\pockets\ErpConf;


class ShopErpInfo extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_erp_info';

    const _SUB_TABLE = false;

    const ERP_SOURCE = ErpConf::API_NAME_SOURCE;
    const ERP_STATUS = ErpConf::API_STATUS;

    public static function getByWhere($method, $fields, $where = '')
    {
        return self::$method($fields, $where);
    }

    public static function saveByWhere($method, $data, $where = '')
    {
        $ret = [];
        //提交字段进行校验
        if (!vShopErpInfo::validate($data, $method)) {
            $msg = vShopErpInfo::firstError();
            throw new \Exception($msg);
        }

        try {
            switch ($method) {
                case 'insert':
                    $ret = self::insert($data);
                    break;
                case 'update':
                    $ret = self::update($data, $where);
            }
            if ($ret->errorCode() !== '00000') {
                throw new \Exception('操作失败,请重试');
            }else{
                return true;
            }

        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * 判断是否开启
     */
    public static function getStatus($where)
    {
        $res = false;
        $shop_erp = self::getByWhere('get', '*', $where);
        if($shop_erp && $shop_erp['status']) {
            $res = true;
        }

        return $res;
    }

}

